// pages/person/index.js
import config from '../../etc/config.js'
const {
  $Toast
} = require('../../components/dist/base/index');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    deId: '',
    show: false
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDeliveryById()
    this.getUserInfo()
  },
  onLoad(options) {
    wx.hideShareMenu()
    this.setData({
      deId:options.deId
    })
    this.getUserInfo()
    this.getDeliveryById()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getUserInfo() {
    let that = this;
    let params = { userId: wx.getStorageSync('userId') }
    let token = wx.getStorageSync('token')
    config._post('/user/getUserInfo', token, params).then(res => {
      if (res.data) {
        // 用户基本信息展示
        that.setData({
          userInfo: res.data
        })
        if (that.data.show) {
          this.getDeliveryById(res.data.deId)
        }
      }
    })
  },
  getDeliveryById() {
    let params = {
      id: this.data.deId
    }
    let token = wx.getStorageSync('token')
    config._post('/deliveryman/getDeliveryInfoById', token, params).then(res => {
      this.setData({
        deliveryById: res.data
      })
    })
  },
  editName: function () {
    wx.navigateTo({
      url: '../edit-person/index?title=姓名' + '&name=' + this.data.userInfo.userName,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  editAvatar() {
    const that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        if (res.tempFiles[0].size / 1024 / 1024 > 2) {
          $Toast({
            content: '请上传大小不超过2M的图片'
          })
          return false
        } else {
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths
          let token = wx.getStorageSync('token')
          wx.uploadFile({
            url: config.basePath + '/file/upload', // 仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              access_token: token
            },
            success(res) {
              const data = JSON.parse(res.data)
              let params = {
                userId: wx.getStorageSync('userId'),
                userHeadUrl: data.data.bucketName + '-' + data.data.fileName
              }
              config._post('/user/updateHead', token, params).then(res => {
                that.getUserInfo()
              })
            }
          })
        }
      }
    })
  },
  editPhone: function () {
    wx.navigateTo({
      url: '../edit-person/index?title=电话' + '&phone=' + this.data.deliveryById.dePhone,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  editSex: function () {
    wx.navigateTo({
      url: '../edit-person/index?title=性别' + '&sex=' + this.data.userInfo.userSex,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  editCardNumber: function () {
    wx.navigateTo({
      url: '../edit-person/index?title=会员卡号' + '&cardNumber=' + this.data.userInfo.userCardNumber,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
})
